home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / scripts / audio / lin2mu.m next >
Text File  |  1996-07-15  |  2KB  |  57 lines

  1. ## Copyright (C) 1996 John W. Eaton
  2. ##
  3. ## This file is part of Octave.
  4. ##
  5. ## Octave is free software; you can redistribute it and/or modify it
  6. ## under the terms of the GNU General Public License as published by
  7. ## the Free Software Foundation; either version 2, or (at your option)
  8. ## any later version.
  9. ##
  10. ## Octave is distributed in the hope that it will be useful, but
  11. ## WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13. ## General Public License for more details.
  14. ##
  15. ## You should have received a copy of the GNU General Public License
  16. ## along with Octave; see the file COPYING.  If not, write to the Free
  17. ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA
  18. ## 02111-1307, USA.
  19.  
  20. ## usage:  y = lin2mu (x)
  21. ##
  22. ## x is a vector of an 8- or 16-bit linearly encoded audio sample,
  23. ## This is transformed into a mu-law encoded vector.
  24.  
  25. ## Author: AW <Andreas.Weingessel@ci.tuwien.ac.at>
  26. ## Created: 17 October 1994
  27. ## Adapted-By: jwe
  28.  
  29. function y = lin2mu (x)
  30.  
  31.   if (nargin != 1)
  32.     usage ("y = lin2mu (x)");
  33.   endif
  34.  
  35.   if (! is_vector (x))
  36.     error ("lin2mu: x must be a vector");
  37.   endif
  38.  
  39.   ## transform 8-bit format to 16-bit
  40.   if (max (abs (x)) <= 128)
  41.     x = 256 .* x;
  42.   endif
  43.  
  44.   ## determine sign of x, set sign(0) = 1.
  45.   sig = sign(x) + (x == 0);
  46.  
  47.   ## take absolute value of x, but force it to be smaller than 32636;
  48.   ## add bias
  49.   x = min (abs (x), 32635 * ones (size (x))) + 132;
  50.  
  51.   ## find exponent and fraction of bineary representation
  52.   [f, e] = log2 (x);
  53.  
  54.   y = 64 * sig - 16 * e - fix (32 * f) + 335;
  55.  
  56. endfunction
  57.